from django.shortcuts import render
from QQLoginTool.QQtool import OAuthQQ
from django.views import View
from django.conf import settings
from django.http import JsonResponse
from .models import OAuthQQUser
from django.contrib.auth import login
from apps.users.models import User
from meiduo_mall.utils.secret import SecretOauth
import json
import re



class QQURLView(View):
    def get(self,request):
        next = request.GET.get('next')

        oauth = OAuthQQ(
            client_id=settings.QQ_CLIENT_ID,
            client_secret=settings.QQ_CLIENT_SECRET,
            redirect_uri=settings.QQ_REDIRECT_URI,
            state=next
        )

        oauth_url = oauth.get_qq_url()

        return JsonResponse({'code':0,'errmsg':'OK','login_url':oauth_url})

class QQUserView(View):
    def get(self,request):
        code = request.GET.get('code')
        oauth = OAuthQQ(
            client_id=settings.QQ_CLIENT_ID,
            client_secret= settings.QQ_CLIENT_SECRET,
            redirect_uri=settings.QQ_REDIRECT_URI,
            state='/'
        )
        try:
            access_token = oauth.get_access_token(code)
            openid = oauth.get_open_id(access_token)
            print(openid)
        except Exception as e:
            print(e)
            return JsonResponse({'code':400,'errmsg':'参数错误'})

        try:
            oauth_user = OAuthQQUser.objects.get(openid=openid)
        except:
            access_token = SecretOauth().dumps({'openid':openid})
            return JsonResponse({'code':400,'errmsg':'用户未绑定','access_token':access_token})
        else:
            login(request,oauth_user.user)
            response = JsonResponse({'code':0,'errmsg':'OK'})
            response.set_cookie('username',oauth_user.user.username,3600*14*24)
            return response

    def post(self,request):
        json_data = request.body.decode()
        json_data = json.loads(json_data)
        mobile = json_data.get('mobile')
        password = json_data.get('password')
        sms_code = json_data.get('sms_code')
        access_token = json_data.get('access_token')
        print(access_token)
        if not all([mobile,password,sms_code,access_token]):
            return JsonResponse({'code':400,'errmsg':'参数缺失'})
        if not re.match('^1[3-9]\d{9}$',mobile):
            return JsonResponse({'code':400,'errmsg':'格式输入错误'})
        if not re.match('\w{8,20}',password):
            return JsonResponse({'code': 400, 'errmsg': '格式输入错误'})

        openid = SecretOauth().loads(access_token).get('openid')
        print('openid',openid)
        if not openid:
            return JsonResponse({'code': 400, 'errmsg': '格式输入错误'})
        try:
            user = User.objects.get(mobile=mobile)
            if not user.check_password(password):
                return JsonResponse({'code': 400, 'errmsg': '输入错误'})
        except Exception as e:
            user = User.objects.create(
                username = mobile,
                mobile=mobile,
                password=password
            )


        OAuthQQUser.objects.create(
            openid=openid,
            user = user
        )
        login(request,user)
        response =  JsonResponse({'code':0,'errmsg':'OK'})
        response.set_cookie('username',user.username)
        return response













